1 00:00:00,610 --> 00:00:01,420 Welcome back. 2 00:00:01,420 --> 00:00:02,500 Hope you're doing well. 3 00:00:02,500 --> 00:00:06,520 In this lecture we're going to be scripting the sounds handler on the client. 4 00:00:06,520 --> 00:00:11,710 And this script will be responsible for muffling different sound groups, fading out sounds, listening 5 00:00:11,740 --> 00:00:15,490 to actions from the server, relating to sounds and stuff like that. 6 00:00:15,490 --> 00:00:19,360 So inside of the service we're going to need replicated storage. 7 00:00:21,650 --> 00:00:24,530 We're going to need the sound service. 8 00:00:26,390 --> 00:00:28,850 We will need the player service. 9 00:00:30,200 --> 00:00:32,900 And we'll also need the tween service. 10 00:00:34,980 --> 00:00:38,520 Now, from this point we can create the table that will represent this module script. 11 00:00:38,520 --> 00:00:40,110 We'll call it sounds handler. 12 00:00:40,970 --> 00:00:43,730 And we'll make sure to return it at the end of our script. 13 00:00:45,870 --> 00:00:50,460 And then a public function we're going to need for this handler is simply an initialize function. 14 00:00:50,460 --> 00:00:53,130 So sounds handler init. 15 00:00:54,620 --> 00:01:00,380 And then some variables we're going to need in here is um one we're going to need a reference to the 16 00:01:00,380 --> 00:01:01,640 local player. 17 00:01:03,970 --> 00:01:08,230 We're going to make a reference to the folder that contains all of the module scripts on the client, 18 00:01:08,230 --> 00:01:09,400 or this modules folder. 19 00:01:09,400 --> 00:01:17,890 So we can call this modules which is equal to script dot parent dot parent dot modules. 20 00:01:18,580 --> 00:01:23,500 We're also going to make some references to different events, such as the game comms event which is 21 00:01:23,500 --> 00:01:27,400 in replicated storage, dot events, dot remotes, dot game communication. 22 00:01:27,400 --> 00:01:30,580 And then we're also going to get the game comms enum. 23 00:01:30,580 --> 00:01:36,310 So we'll require this from updated storage dot modules dot enums dot game comms enum. 24 00:01:37,110 --> 00:01:41,790 And then I'm going to make a reference to all of the different lightning sounds that are inside of the 25 00:01:41,790 --> 00:01:42,540 sound service. 26 00:01:42,540 --> 00:01:48,090 So lightning sounds is equal to sound service, dot ambience, dot weather, dot thunder. 27 00:01:48,090 --> 00:01:52,200 And we're going to get all of the children in there because we're going to listen to when our game comms 28 00:01:52,200 --> 00:01:52,860 event is fired. 29 00:01:52,860 --> 00:01:57,630 And that will tell us to play lightning sounds, which we'll do by collecting all of the lightning sounds 30 00:01:57,630 --> 00:01:59,100 and playing a random one. 31 00:01:59,220 --> 00:02:04,560 And then we're actually going to require a module that we've created inside of the modules folder called 32 00:02:04,560 --> 00:02:05,310 lightning. 33 00:02:05,310 --> 00:02:11,220 And this module script will just have one function inside of it called Summon Lightning to apply. 34 00:02:11,220 --> 00:02:14,940 You know that lightning effect to the lightning in our game. 35 00:02:15,060 --> 00:02:20,430 So we'll call this lightning and we'll require it from modules dot lightning. 36 00:02:20,550 --> 00:02:24,510 And inside of here we can create a variable. 37 00:02:24,510 --> 00:02:26,850 We'll just call it lightning. 38 00:02:27,740 --> 00:02:30,800 And we'll make sure to return it at the end of this script here. 39 00:02:32,330 --> 00:02:37,670 And all this module script is going to have is a function to summon lightning. 40 00:02:39,470 --> 00:02:42,140 And we can create a private function for doing that. 41 00:02:46,390 --> 00:02:48,070 Now back to our sounds handler. 42 00:02:48,070 --> 00:02:52,360 Some other variables we're going to need are some references to parts in the workspace. 43 00:02:52,360 --> 00:02:55,090 So I actually have a few invisible parts. 44 00:02:55,090 --> 00:02:57,610 One is right here, one is right here. 45 00:02:57,610 --> 00:03:01,780 And then we have another invisible part down here in the basement somewhere. 46 00:03:01,780 --> 00:03:03,070 Yes, this one right here. 47 00:03:03,070 --> 00:03:06,760 And these are the different parts that are going to be on our map that when our player touches it, 48 00:03:06,760 --> 00:03:10,120 we're going to change the different sound groups. 49 00:03:10,120 --> 00:03:13,660 So we have an indoor, outdoor and basement sound group. 50 00:03:14,170 --> 00:03:20,320 So when we touch this outdoor part what we need to do is we need to make the outdoor not muffled. 51 00:03:20,320 --> 00:03:23,830 So this equalizer sound effect has to stay disabled. 52 00:03:23,830 --> 00:03:28,480 And we want to enable the equalizer sound effect on the basement and on the indoor sounds. 53 00:03:28,480 --> 00:03:34,420 And then when we go inside, we want to disable the equalizer sound effect on the indoor sounds and 54 00:03:34,420 --> 00:03:36,850 enable it for the basement and outdoor sounds. 55 00:03:36,850 --> 00:03:43,090 That way any sounds playing indoors will be clear, and any sounds playing outdoor or in the basement 56 00:03:43,090 --> 00:03:45,280 will sound muffled like they're coming through a wall. 57 00:03:45,670 --> 00:03:47,950 So we can make references to those parts. 58 00:03:47,950 --> 00:03:53,350 We can have one called Inside Touch part, which is in the workspace filtering folder. 59 00:03:53,350 --> 00:03:55,960 Dot touch parts dot inside. 60 00:03:55,960 --> 00:03:59,710 We can make a reference to the outside touch part. 61 00:04:01,320 --> 00:04:07,950 That touch parts outside, and then we can have a reference to the basement touch part. 62 00:04:08,510 --> 00:04:12,200 Workspace, filtering folder, touch parts basement. 63 00:04:12,380 --> 00:04:15,500 And then I'm going to create a random data type as well. 64 00:04:16,040 --> 00:04:17,960 Now some functions we're going to need in here. 65 00:04:17,960 --> 00:04:19,580 One is to fade out sound. 66 00:04:19,580 --> 00:04:22,310 So I can call this fade sound out. 67 00:04:22,310 --> 00:04:25,610 And this function will be responsible for getting a sound. 68 00:04:25,610 --> 00:04:29,960 And if the sound is playing then we're going to fade out the sound using the tween service. 69 00:04:29,960 --> 00:04:34,940 We want to have a function to set all of the audio in our game to basement. 70 00:04:34,940 --> 00:04:40,400 Or basically we want to make any sounds in the basement sound clear, and any sounds that are not in 71 00:04:40,400 --> 00:04:41,960 the basement to sound muffled. 72 00:04:41,960 --> 00:04:48,590 We want to have a function for doing the same thing to set the audio to inside, and then we can have 73 00:04:48,590 --> 00:04:52,100 a function to set the audio to outside. 74 00:04:53,630 --> 00:04:59,660 Because again, each of these different sounds that are inside of our sound service, such as the ambience, 75 00:04:59,660 --> 00:05:06,140 if we go to let me find weather and we go to like a rain or thunder, you can see their sound group 76 00:05:06,140 --> 00:05:07,310 is set to outdoor. 77 00:05:07,310 --> 00:05:11,840 So that means when we muffle the outdoor sounds then these will sound a little bit different. 78 00:05:11,840 --> 00:05:14,960 So actually what I'm going to do here is we're going to play rain normally. 79 00:05:14,960 --> 00:05:17,420 So when we're out sound it should sound like this. 80 00:05:19,420 --> 00:05:23,770 And then when we go inside, this equalizer sound effect will get enabled. 81 00:05:23,770 --> 00:05:26,140 And then our rain should sound like this. 82 00:05:28,700 --> 00:05:31,160 So as you can see, it sounds like we're inside of a building now. 83 00:05:31,190 --> 00:05:32,690 Same thing goes with the thunder. 84 00:05:32,690 --> 00:05:37,940 So if I disable this and play like this one or this one. 85 00:05:41,150 --> 00:05:46,520 And then I go and enable these, uh, sound effects and then try to play it again. 86 00:05:48,680 --> 00:05:51,200 As you can see, it sounds like it's a little bit muffled now. 87 00:05:52,700 --> 00:05:56,480 So that means in each one of these functions, we're going to have to do is we're going to have to reference 88 00:05:56,480 --> 00:05:57,740 our sound service. 89 00:05:57,740 --> 00:06:02,450 And when we want to set the audio to basement, what we could do is we could reference our indoor sound 90 00:06:02,450 --> 00:06:07,040 group and we could get the equalizer sound effect in there and set the enable property equal to true. 91 00:06:07,040 --> 00:06:08,390 Because we're in the basement. 92 00:06:08,390 --> 00:06:14,540 We can also do this for the sound service basement, get the equalizer sound effect in there and set 93 00:06:14,540 --> 00:06:18,170 the enable property to false because we are now inside of the basement. 94 00:06:18,170 --> 00:06:23,390 And then we want to set the sound service dot basement dot reverb sound effect that's in there. 95 00:06:23,390 --> 00:06:24,890 We want to make sure that's enabled. 96 00:06:24,890 --> 00:06:31,310 So inside of basement sounds when this reverb is applied that means we're going to have an echo sound 97 00:06:31,310 --> 00:06:32,630 effect because we're in the basement. 98 00:06:32,630 --> 00:06:33,140 Right. 99 00:06:33,530 --> 00:06:40,370 We also want to make sure to set the sound service, the outdoor sound group reverb or I mean equalizer 100 00:06:40,370 --> 00:06:42,830 sound effect dot enabled equal to true. 101 00:06:43,550 --> 00:06:49,490 And I also want to reduce the volume of any sounds that come from outside or come from inside of the 102 00:06:49,490 --> 00:06:49,820 building. 103 00:06:49,820 --> 00:06:56,600 So we can reference the Sound Service, Inc. or Sound Group and set the volume equal to something like 104 00:06:56,600 --> 00:06:57,740 0.5. 105 00:06:57,740 --> 00:07:03,800 And then for the sound service outdoor volume, since we're in the basement or basically underground, 106 00:07:03,800 --> 00:07:08,780 we want to set this something to a low value so we could do something like 0.05. 107 00:07:09,080 --> 00:07:14,360 Now the volume property inside of sound groups actually affects all of the volumes. 108 00:07:14,360 --> 00:07:19,970 For any sounds that have this, uh, sound group property set to like, for example, the outdoor sound 109 00:07:19,970 --> 00:07:20,480 group. 110 00:07:20,480 --> 00:07:25,550 If I were to set this volume to zero, that means we're not going to be able to hear any of the sounds 111 00:07:25,550 --> 00:07:28,220 coming out of these different, uh, sound instances. 112 00:07:28,220 --> 00:07:32,000 So if I play this one, as you can see, there's no sound at all. 113 00:07:32,150 --> 00:07:38,870 This volume property universally affects any sound instances that have this sound group set for their 114 00:07:38,870 --> 00:07:40,070 sound group property. 115 00:07:40,070 --> 00:07:46,220 So that means if I decrease it, then it's going to make it, you know, reduce for the volume of these 116 00:07:46,220 --> 00:07:47,600 different sound effects. 117 00:07:48,170 --> 00:07:56,240 So a volume of one basically means that these different sound effects, uh, their volumes are not affected. 118 00:07:56,240 --> 00:07:56,780 Right. 119 00:07:56,780 --> 00:08:02,090 When we have a one value, that means the volume that's set in their property, like 0.5 stays the same. 120 00:08:02,090 --> 00:08:07,310 But if we decrease it to something like 0.5, then that means the volume of each of these sound instances 121 00:08:07,310 --> 00:08:09,080 has their volume cut in half. 122 00:08:09,080 --> 00:08:13,220 So instead this is now acting as a volume of like 0.25. 123 00:08:13,820 --> 00:08:17,840 So the volume got reduced, as you can see. 124 00:08:18,860 --> 00:08:22,970 And in fact an outdoor volume of 0.05 might be a little low. 125 00:08:22,970 --> 00:08:25,430 So we could set this to something like 0.1 instead. 126 00:08:26,280 --> 00:08:30,780 And then when we want to set the audio to be inside, we can just copy all of this. 127 00:08:30,960 --> 00:08:36,270 And what we want to do is we want to set the indoor equalizer sound effect enabled to false. 128 00:08:36,810 --> 00:08:39,570 We can change this to be true. 129 00:08:39,600 --> 00:08:42,990 We're going to disable the reverb sound effect in the basement. 130 00:08:43,320 --> 00:08:48,600 Uh, the outdoor sound effect, uh, equalizer sound effect is going to stay to true, and then the 131 00:08:48,600 --> 00:08:50,820 indoor volume can be set back to one. 132 00:08:50,820 --> 00:08:54,180 The outdoor volume can be set to something like 0.5. 133 00:08:54,450 --> 00:09:02,700 And that also means we should reference the sound service dot basement dot volume and set that to something 134 00:09:02,700 --> 00:09:03,540 like one. 135 00:09:03,540 --> 00:09:06,030 When we're in the basement and when we are not in the basement. 136 00:09:06,030 --> 00:09:12,000 So when we are inside, we can set this to a volume of something like 0.5. 137 00:09:12,510 --> 00:09:14,670 And then we can copy this one more time. 138 00:09:14,670 --> 00:09:19,140 And then when we want to set the audio to outside the equalizer, sound effect for indoors will be set 139 00:09:19,140 --> 00:09:19,800 to true. 140 00:09:19,830 --> 00:09:24,870 So for the basement and then for the outdoor sounds we're going to set this one to false. 141 00:09:24,990 --> 00:09:28,110 Basement sounds can basically be set to like 0.1. 142 00:09:28,110 --> 00:09:32,640 Indoor sounds can be set to 0.5, and then outdoor volume can be set to one. 143 00:09:33,120 --> 00:09:36,780 And while we're at it, I guess we could also fill out our fade sound out function. 144 00:09:36,780 --> 00:09:40,050 So this function will get passed a sound instance. 145 00:09:41,040 --> 00:09:43,830 And we first want to check whether or not the sound is playing. 146 00:09:43,830 --> 00:09:48,510 So if the sound is not playing, then we're just going to return out of the function. 147 00:09:48,510 --> 00:09:51,870 Otherwise we can create a tween using the tween service. 148 00:09:51,870 --> 00:09:56,910 On this particular sound instance, we can create a tween info of something like one second. 149 00:09:57,520 --> 00:10:02,050 And we want to fade out this sound so we can set the volume property equal to zero. 150 00:10:02,230 --> 00:10:04,510 And then we play the tween. 151 00:10:04,510 --> 00:10:06,910 And then we're going to wait for the tween to complete. 152 00:10:06,910 --> 00:10:12,580 And then once it completes, then we're just going to stop the sound from playing any further. 153 00:10:13,440 --> 00:10:18,480 Another private function we're going to need in here is to listen to our game comms event, so we can 154 00:10:18,480 --> 00:10:21,990 call this function on game comms event. 155 00:10:21,990 --> 00:10:24,960 We get passed in action and arguments with that action. 156 00:10:24,960 --> 00:10:29,190 And then inside of our initialize function, what we could do in here is we could listen to our games 157 00:10:29,190 --> 00:10:34,110 comm event dot on client event and connect our on game comms event function. 158 00:10:34,110 --> 00:10:38,790 And then we also want to listen for when our outside touch part gets touched. 159 00:10:39,390 --> 00:10:44,850 Connect the Lambda function to that we want to listen to when our inside touch part gets touched. 160 00:10:46,450 --> 00:10:51,400 And we also want to listen to win our basement touch part gets touched. 161 00:10:52,680 --> 00:10:58,500 And each of these lambda functions will get past the part that touched our outside, inside and basement 162 00:10:58,530 --> 00:11:01,350 touch parts so we can call it other part. 163 00:11:02,980 --> 00:11:06,820 Now we can go ahead and fill out these different, uh, functions here real quick. 164 00:11:06,820 --> 00:11:12,490 So when our outside touch part gets touched, we first want to verify that this part that touched it 165 00:11:12,490 --> 00:11:15,070 was actually from our character model. 166 00:11:15,070 --> 00:11:19,540 So we can check first off if the other part dot parent. 167 00:11:20,140 --> 00:11:22,090 Find first child, which is a humanoid. 168 00:11:22,090 --> 00:11:25,540 So that means the part that touched this belonged to a character model. 169 00:11:26,410 --> 00:11:31,270 So if it did belong to a character model, then we want to check exactly who touched it, which player 170 00:11:31,270 --> 00:11:31,900 touched it. 171 00:11:31,900 --> 00:11:37,660 If the player that touched it is not our local player, then we want to ignore this event so players 172 00:11:37,660 --> 00:11:42,460 we can use the function, get player from character and pass this other part dot parent. 173 00:11:42,670 --> 00:11:47,830 And if this player is not equal to our local player, then we're just going to return because we only 174 00:11:47,830 --> 00:11:50,860 want to listen for when our player touches it. 175 00:11:50,860 --> 00:11:57,550 However, if we did touch it, then we can call our set audio to outside function when we touch the 176 00:11:57,550 --> 00:11:58,540 outside part. 177 00:11:58,540 --> 00:12:01,870 And then we could just basically copy this, paste it in here. 178 00:12:01,870 --> 00:12:05,320 And then for this one we want to set the audio to inside. 179 00:12:06,070 --> 00:12:08,260 So set audio to inside. 180 00:12:08,680 --> 00:12:14,740 And then for this one we'll just copy all of this and set the audio to the basement. 181 00:12:15,600 --> 00:12:19,530 The last function we can go ahead and fill out is our on game comms event. 182 00:12:19,560 --> 00:12:24,390 So inside of this function, the different actions we want to listen to are inside of our game comms 183 00:12:24,390 --> 00:12:24,720 enum. 184 00:12:24,720 --> 00:12:29,550 So if the action is equal to game comms enum dot two client. 185 00:12:29,550 --> 00:12:31,830 And the first one we could do is play sound. 186 00:12:32,390 --> 00:12:37,790 So if the action is play sound, then what we could do is we could give the server the option to let 187 00:12:37,790 --> 00:12:43,520 us know whether or not we want this sound to play at a particular point on our map, or we just want 188 00:12:43,520 --> 00:12:46,010 to play the sound universally for the player. 189 00:12:46,010 --> 00:12:51,830 So basically, does the server want us to have this sound emanate from a specific point, or do we just 190 00:12:51,830 --> 00:12:55,010 want to play the sound, for example, directly from the sound service? 191 00:12:55,010 --> 00:13:01,640 So what we could do is we could have a key value pair inside of our args table that defines whether 192 00:13:01,640 --> 00:13:03,050 or not we want to play it at a point. 193 00:13:03,050 --> 00:13:07,250 So if not ARGs and we could do play at point. 194 00:13:07,580 --> 00:13:12,530 So if we do not need to play the sound at a particular point, then we could just do args and we could 195 00:13:12,530 --> 00:13:15,650 have another key in there that represents the sound instance. 196 00:13:15,650 --> 00:13:18,710 So we could just call it sound and then we play the sound. 197 00:13:19,250 --> 00:13:23,840 Otherwise that means we're going to have to play the sound at a particular point so we can create a 198 00:13:23,840 --> 00:13:28,760 part in the workspace using the instance dot new function and create a new part. 199 00:13:30,540 --> 00:13:32,490 We're going to make this part anchored. 200 00:13:32,490 --> 00:13:38,010 We're going to set can collide equal to false part dot transparency. 201 00:13:38,040 --> 00:13:39,930 We're going to set equal to one. 202 00:13:39,930 --> 00:13:44,430 And then the position for this part is going to be supplied by the server. 203 00:13:44,430 --> 00:13:48,420 So we can have another key value pair in the arguments table called position. 204 00:13:49,380 --> 00:13:53,040 And then we need to set the parent of this part equal to the workspace. 205 00:13:53,040 --> 00:13:55,530 And then we need to create a clone of the sound. 206 00:13:55,530 --> 00:14:00,150 So we can call this sound clone equal to args dot sound clone. 207 00:14:00,850 --> 00:14:04,300 Set the parent of our sound clone. 208 00:14:05,440 --> 00:14:07,030 Equal to this part. 209 00:14:07,030 --> 00:14:10,450 And then we're going to get our sound clone and play it. 210 00:14:10,780 --> 00:14:14,530 We're going to wait for this sound clone to end. 211 00:14:15,470 --> 00:14:19,670 And then once the sound finishes playing, then we can go ahead and destroy this part. 212 00:14:20,340 --> 00:14:21,270 Perfect. 213 00:14:21,420 --> 00:14:27,510 Another action we want to listen to is when the server wants us to fade out a particular sound. 214 00:14:27,510 --> 00:14:35,640 So if the action is to stop a sound, then what we could do is we could call our fade sound out function 215 00:14:35,640 --> 00:14:38,640 and pass args dot sound. 216 00:14:39,120 --> 00:14:41,370 Um, another action we want to listen to. 217 00:14:42,490 --> 00:14:50,440 In our Gamescom enum two client is when the server wants us to change our sound to inside, for example, 218 00:14:50,440 --> 00:14:55,900 because what we're going to do is that when this part right here gets touched, we want to teleport 219 00:14:55,900 --> 00:14:57,940 all of the players inside of the restaurant. 220 00:14:57,940 --> 00:15:03,340 And when we do that, not all of the players are going to be able to touch this part unless they actually 221 00:15:03,340 --> 00:15:05,230 come and walk over and touch this part. 222 00:15:05,230 --> 00:15:12,010 So that means the one player that touches this part automatically has their sound groups set up correctly, 223 00:15:12,010 --> 00:15:16,840 but all the other players that get teleported in here won't have their sound groups set up correctly. 224 00:15:16,840 --> 00:15:22,780 So we need to tell those players through this, uh, game comms event to, you know, change their sound 225 00:15:22,780 --> 00:15:23,830 to be inside. 226 00:15:23,830 --> 00:15:25,840 And this is going to be pretty easy. 227 00:15:25,840 --> 00:15:29,710 All we can do is just call our set audio to inside function. 228 00:15:30,220 --> 00:15:33,550 And then we need to do the same thing for the basement. 229 00:15:34,130 --> 00:15:40,760 So if the action is equal to change sound to basement, then we can just call our set audio to basement 230 00:15:40,760 --> 00:15:41,510 function. 231 00:15:41,510 --> 00:15:46,850 And then the last action we want to listen to is when the server tells us to play some lightning. 232 00:15:47,750 --> 00:15:49,790 So if the action is lightning. 233 00:15:50,590 --> 00:15:53,980 Then what we could do is we could use our lightning module script. 234 00:15:54,730 --> 00:16:00,940 And call our Summon Lightning function, which will help to flicker the brightness in the game. 235 00:16:00,940 --> 00:16:07,480 And then we're going to wait for a random amount of time using the RNG next integer, or we can use 236 00:16:07,480 --> 00:16:08,260 next number. 237 00:16:08,260 --> 00:16:11,110 Wait anywhere from between 0.4 to 2 seconds. 238 00:16:11,500 --> 00:16:17,590 And then what we could do is we could get our lightning sounds table, and we want to play a random 239 00:16:17,590 --> 00:16:18,880 one of these lightning sounds. 240 00:16:18,880 --> 00:16:25,540 So we can use RNG next integer and get a random sound between the index of one to the max number of 241 00:16:25,540 --> 00:16:29,680 lightning sounds, and then we just play this particular sound. 242 00:16:30,450 --> 00:16:34,080 And that means we should go ahead and fill out our Summon Lightning function. 243 00:16:34,410 --> 00:16:39,750 What's going to happen here is when we are told to summon lightning, we're going to call our Summon 244 00:16:39,750 --> 00:16:41,310 Lightning private function. 245 00:16:41,310 --> 00:16:45,900 And inside of here, what we could do is we could get a random, uh, number. 246 00:16:45,900 --> 00:16:51,450 So random num is equal to RNG next number between 8 and 12. 247 00:16:51,450 --> 00:16:58,020 So we're basically doing the exact same thing that we did within our, uh, other module script that 248 00:16:58,020 --> 00:16:59,130 was in our lobby. 249 00:16:59,400 --> 00:17:02,550 And that means we're going to need to get our lighting service in here. 250 00:17:02,550 --> 00:17:07,770 So lighting is equal to game get service lighting. 251 00:17:08,220 --> 00:17:10,950 We're of course going to need our RNG data type. 252 00:17:10,950 --> 00:17:13,980 So local RNG is equal to random dot new. 253 00:17:13,980 --> 00:17:18,210 And then I want to make a reference to the original brightness in our game. 254 00:17:18,210 --> 00:17:21,990 So that's in the lighting dot brightness. 255 00:17:22,320 --> 00:17:26,130 And then inside of here we're going to do the exact same thing that we did last time. 256 00:17:26,130 --> 00:17:33,390 We're going to refer to the lighting service and set the brightness plus equal this random number. 257 00:17:34,020 --> 00:17:36,840 Well wait a random amount of time using RNG. 258 00:17:36,870 --> 00:17:41,820 Next number 0.0820.12. 259 00:17:42,570 --> 00:17:44,130 Copy this again. 260 00:17:44,160 --> 00:17:45,000 Put that here. 261 00:17:45,000 --> 00:17:48,330 But this time we want to subtract random number by four. 262 00:17:48,330 --> 00:17:52,170 And we want to set the brightness minus equal this random number. 263 00:17:53,080 --> 00:17:55,660 Basically just copy this same thing here again. 264 00:17:55,660 --> 00:18:01,510 This time we want to add brightness and we'll add six to random number. 265 00:18:01,870 --> 00:18:02,980 Paste it again. 266 00:18:02,980 --> 00:18:06,580 And this time we can wait a little longer up to 0.15 seconds. 267 00:18:06,580 --> 00:18:09,100 And this time we can also add as well. 268 00:18:09,100 --> 00:18:11,260 But we want to add a slightly smaller number. 269 00:18:11,260 --> 00:18:14,050 So we can subtract random num minus eight. 270 00:18:14,050 --> 00:18:16,240 So actually this will be a little random. 271 00:18:16,240 --> 00:18:19,300 If random number is eight then the brightness won't increase. 272 00:18:19,300 --> 00:18:21,250 So actually let me change this number to like ten. 273 00:18:21,370 --> 00:18:22,630 So basically. 274 00:18:23,300 --> 00:18:28,370 We increase the brightness, and then here the brightness may increase more, or the brightness might 275 00:18:28,370 --> 00:18:31,880 decrease a little depending on what random number gets generated here. 276 00:18:32,480 --> 00:18:38,450 And then after that, we're of course going to wait a little bit longer again and then set the lighting 277 00:18:38,450 --> 00:18:39,380 service. 278 00:18:39,380 --> 00:18:45,500 So lighting brightness equal to the original brightness that we stored here originally. 279 00:18:46,170 --> 00:18:49,830 So our sound handler should be all good set up and ready to go. 280 00:18:49,830 --> 00:18:55,290 So that means if we go to test out our main story, if we touch each one of these parts, we should 281 00:18:55,290 --> 00:18:59,400 have the sound groups in our game muffled to their respective areas. 282 00:18:59,400 --> 00:19:01,050 So let's go ahead and try it out. 283 00:19:01,950 --> 00:19:07,380 So when we go to touch this outside part, as you can see our sound doesn't change. 284 00:19:07,380 --> 00:19:08,880 We can still hear the rain just fine. 285 00:19:08,880 --> 00:19:14,640 But if we open our door and we go inside, as you can see immediately we do not hear the rain anymore. 286 00:19:14,640 --> 00:19:17,730 It's become muffled and the volume from outside has decreased. 287 00:19:17,730 --> 00:19:22,110 But if we go and touch the outside part again, as you can see, we can hear the rain again. 288 00:19:25,830 --> 00:19:27,060 And oops. 289 00:19:27,060 --> 00:19:30,780 Unfortunately, it seems like our door is locked here. 290 00:19:30,780 --> 00:19:35,040 So what I could do is real quick is I'll just grab my character and put him in here from the server, 291 00:19:35,040 --> 00:19:36,600 and then we'll go back to the client. 292 00:19:36,600 --> 00:19:39,600 And then if I go and touch my basement sound. 293 00:19:41,810 --> 00:19:44,090 I do not believe we can hear the rain anymore. 294 00:19:44,090 --> 00:19:46,190 So our rain has completely disappeared. 295 00:19:46,190 --> 00:19:49,220 And now we are inside of our lovely basement. 296 00:19:51,050 --> 00:19:55,490 And unfortunately, it looks like we're getting some rain passed through our basement. 297 00:19:55,490 --> 00:19:59,930 We can go ahead and fix that by adding an invisible part above the basement. 298 00:19:59,930 --> 00:20:01,520 So let's go ahead and fix that real quick. 299 00:20:02,520 --> 00:20:07,770 So it's actually go ahead and move our rain blocker for the basement because apparently it's not working 300 00:20:07,770 --> 00:20:08,460 right now. 301 00:20:08,460 --> 00:20:12,870 And to make sure that we can actually see what the heck we're doing, we're going to change the brightness 302 00:20:12,870 --> 00:20:15,000 equal to something like. 303 00:20:15,770 --> 00:20:17,810 We could just do like ten or something like that. 304 00:20:17,810 --> 00:20:21,590 And then let me, uh, make the fog, like, super far away. 305 00:20:21,590 --> 00:20:23,420 That way you can actually see what's going on. 306 00:20:23,420 --> 00:20:24,260 Okay. 307 00:20:24,500 --> 00:20:26,900 We can get our rain blocker for the basement. 308 00:20:26,900 --> 00:20:34,490 Let me move this down and actually let me change the transparency of our rain blocker to something like 309 00:20:34,490 --> 00:20:35,660 0.98. 310 00:20:35,660 --> 00:20:38,630 I don't remember exactly what the threshold was for our rain. 311 00:20:38,630 --> 00:20:39,950 So let me check real quick. 312 00:20:40,100 --> 00:20:47,780 The transparency threshold is 0.98 and we can actually change this maybe back to 0.99 something like 313 00:20:47,780 --> 00:20:48,350 that. 314 00:20:49,330 --> 00:20:52,060 Because the transparency of this one is. 315 00:20:52,880 --> 00:20:54,770 This one's 0.99 as well. 316 00:20:54,770 --> 00:20:57,770 So we could just set this to 0.98. 317 00:20:59,090 --> 00:21:02,780 And our basement goes basically all along down here. 318 00:21:02,780 --> 00:21:07,340 So we need to resize this to appropriately cover our entire basement. 319 00:21:08,780 --> 00:21:10,940 We can extend it to, like over here. 320 00:21:13,230 --> 00:21:16,950 And then we can have it go all the way down to, like, right here. 321 00:21:20,940 --> 00:21:25,080 And it seems like we need to have it continue going this way. 322 00:21:25,110 --> 00:21:28,950 So what I can do is I can increase the height a little bit. 323 00:21:30,170 --> 00:21:32,360 And then I'll duplicate this. 324 00:21:33,350 --> 00:21:38,690 And let me resize this one here and move it out to cover our entire basement. 325 00:21:39,230 --> 00:21:41,600 So we'll do something like this. 326 00:21:43,210 --> 00:21:46,570 And then we'll make sure it doesn't interfere with the restaurant too much. 327 00:21:46,570 --> 00:21:49,120 So we'll have it go like right there. 328 00:21:53,260 --> 00:21:55,960 And that should cover our basement well enough. 329 00:21:55,960 --> 00:21:59,050 So actually, let's go ahead and find out real quick. 330 00:22:00,610 --> 00:22:03,730 Fortunately, it looks like we're still getting rain in our basement. 331 00:22:04,280 --> 00:22:04,580 All right. 332 00:22:04,580 --> 00:22:06,980 I figured out the issue for our brain blockers. 333 00:22:06,980 --> 00:22:10,220 It's because we forgot to enable, uh, can collide on them. 334 00:22:10,220 --> 00:22:15,170 So we got to make sure can collide is enabled for our brain blockers. 335 00:22:15,170 --> 00:22:19,040 And that means now if we go test it, we shouldn't have any more rain in the basement. 336 00:22:20,360 --> 00:22:23,660 Okay, so if we now go back into our basement. 337 00:22:24,500 --> 00:22:25,100 Perfect. 338 00:22:25,100 --> 00:22:26,360 Our rain is gone. 339 00:22:26,480 --> 00:22:28,850 We do not have rain any longer. 340 00:22:28,850 --> 00:22:31,820 Uh, passing through the ceiling of our basement. 341 00:22:32,330 --> 00:22:37,520 Now that we've actually set up the sound handler on the client, we can actually go back to our door 342 00:22:37,520 --> 00:22:43,250 service here, and we can go ahead and update our play sound function to go ahead and utilize that, 343 00:22:43,250 --> 00:22:45,080 uh, sound handler on the client. 344 00:22:45,080 --> 00:22:49,430 So that means up here we're going to have to create a reference to our game comms event. 345 00:22:49,430 --> 00:22:56,030 So game comms event is equal to replicated storage dot events dot remotes dot game communication. 346 00:22:56,030 --> 00:22:59,180 And then we can also go ahead and get our game comms enum. 347 00:23:00,380 --> 00:23:02,090 Replicated storage modules. 348 00:23:02,300 --> 00:23:05,780 Enums dot game communication enum. 349 00:23:06,050 --> 00:23:11,210 And then inside of our play sound function, what we could do is we could use our game comms event, 350 00:23:11,210 --> 00:23:16,610 and we could fire to all of the clients that we would like for them to do a specific action. 351 00:23:16,610 --> 00:23:19,460 And that action is going to be to play a sound. 352 00:23:19,460 --> 00:23:23,360 And then the arguments we can pass here, one is going to be the sound instance. 353 00:23:23,360 --> 00:23:27,230 So we can create a key for the sound, which will be the sound instance. 354 00:23:27,320 --> 00:23:32,720 We can create a key of if we would like the sound to play at a point which we would like for them to 355 00:23:32,720 --> 00:23:32,990 do. 356 00:23:32,990 --> 00:23:39,170 So we could do play at point equal to true, and the point that we would like for them to play it at 357 00:23:39,170 --> 00:23:42,800 is going to be equal to this parent. 358 00:23:42,800 --> 00:23:49,040 So the parent we're going to get this parent and we're going to get the position. 359 00:23:49,880 --> 00:23:52,580 So we can go ahead and delete this extra stuff here. 360 00:23:52,580 --> 00:23:55,970 And we can go ahead and now test our event out. 361 00:23:59,300 --> 00:24:01,910 So if we go to open the door, let's see what happens. 362 00:24:02,900 --> 00:24:03,440 And perfect. 363 00:24:03,440 --> 00:24:09,740 The sound still works just as normal, but instead of playing the sound from the server directly, we're 364 00:24:09,740 --> 00:24:14,390 firing an event and the player is doing it, or the client is doing it on their end instead. 365 00:24:15,320 --> 00:24:18,680 So it appears that our game is coming together nicely so far. 366 00:24:18,680 --> 00:24:21,950 Keep up the good work and I will get to see you in the next lecture.